package com.example.hanqinshan.gifttalk;


import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.hanqinshan.gifttalk.category.CategoryFragment;
import com.example.hanqinshan.gifttalk.home.HomeFragment;
import com.example.hanqinshan.gifttalk.profile.ProfileFragment;
import com.example.hanqinshan.gifttalk.rank.RankFragment;






    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

        //首页下面的四个按钮
        private View mTabHome;
        private View mTabRank;
        private View mTabCategory;
        private View mTabProfile;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            initView();
        }

        private void initView() {
            //初始化按钮控件
            mTabHome = findViewById(R.id.tab_home);
            mTabRank = findViewById(R.id.tab_rank);
            mTabCategory = findViewById(R.id.tab_category);
            mTabProfile = findViewById(R.id.tab_profile);

            //为按钮控件设置点击事件
            mTabHome.setOnClickListener(this);
            mTabRank.setOnClickListener(this);
            mTabCategory.setOnClickListener(this);
            mTabProfile.setOnClickListener(this);
            //通过代码来点击第一个按钮（首页按钮）
            mTabHome.performClick();
        }

        @Override
        public void onClick(View v) {
            //将点击的按钮的状态设置为ture
            resetTabStatus();
            v.setSelected(true);
            //定义Fragment 管理器,用来显示某个Fragment
            FragmentManager fm = getSupportFragmentManager();
            //定义Fragment事务,用来判断显示或隐藏或判断某个Fragment
            FragmentTransaction ft = fm.beginTransaction();
            switch (v.getId()) {
                case R.id.tab_home:
                    ft.replace(R.id.fragment_content, new HomeFragment());
                    break;
                case R.id.tab_rank:
                    ft.replace(R.id.fragment_content, new RankFragment());
                    break;
                case R.id.tab_category:
                    ft.replace(R.id.fragment_content, new CategoryFragment());
                    break;
                case R.id.tab_profile:
                    ft.replace(R.id.fragment_content, new ProfileFragment());
                    break;
            }
            //提交事务,上面的显示功能还会生效
            ft.commit();
        }

        //用来重置所有按钮的状态 把控件 的选择状态设置为Flase
        private void resetTabStatus() {
            mTabHome.setSelected(false);
            mTabRank.setSelected(false);
            mTabCategory.setSelected(false);
            mTabProfile.setSelected(false);
        }

    }

